﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AyFrame.Common.Extensions;
using AyFrame.Dto;
using AyFrame.IServices;
using AyFrame.NSerialLog;
using Microsoft.AspNetCore.Authentication.JwtBearer;

namespace AyFrame.WebApi.Controllers.Basic
{
    /// <summary>
    /// 国家省市区
    /// </summary>
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    [Route("api/area")]
    public class AreaController : BaseController
    {
        readonly IAreaService _areaSer;
        public AreaController(IAreaService areaService)
        {
            _areaSer = areaService;
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        [Route("getall"), HttpGet]
        public IActionResult GetAll()
        {
            return Success(_areaSer.GetAll());
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [Route("add"), HttpPost]
        public async Task<IActionResult> Add(AreaCreateDto dto)
        {
            LogHelper.LogInfo(JsonConvert.SerializeObject(dto), "api/area/add");

            var id = await _areaSer.Add(UserId, dto);
            if (id.IsNullOrEmpty())
                return Fail();

            return Success(id);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [Route("update"), HttpPost]
        public async Task<IActionResult> Update(AreaUpdateDto dto)
        {
            LogHelper.LogInfo(JsonConvert.SerializeObject(dto), "api/area/update");

            var area = await _areaSer.Update(UserId, dto);

            return Success(area);
        }
    }
}
